<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: PollPortlet.php 289 2008-11-14 05:34:40Z steen.rabol@gmail.com $
 * @package Pradoportal.Porttlets.PollPortlet.Edit
 *
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Portlets.PollPortlet.Common.PollPortletRecord');

class PollPortlet extends Portlet
{
	private	$_pollchoices	= null;

	public function dataBind()
	{
		$this->tbColor->Text				= $this->Poll->barcolor;
		$this->Expire->Timestamp			= ($this->Poll->expire ? $this->Poll->expire : time() + (60 * 60 * 24 * 90));
		$this->tbQuestion->Text				= $this->Poll->question;
		$this->Active->Checked				= TPropertyValue::ensureBoolean($this->Poll->active);
		$pollid								= $this->Poll->id;
		$this->dlPollChoices->DataSource	= $this->DataAccess->createCommand("select * from tblpollchoices where pollid=$pollid order by displayorder")->query();
		$this->dlPollChoices->dataBind();
	}

	public function OnLoad($parm)
	{
		$this->Poll	= PollPortletRecord::finder()->findBycontentid($this->Parameters['id']);

		if(!$this->Poll)
		{
			$this->Poll	= new PollPortletRecord();
			$this->Poll->contentid	= $this->Parameters['id'];
			$this->Poll->createdby	= $this->User->Id;
			$this->Poll->question	= Prado::localize("Question");
			$this->Poll->expire		= time() + (60*60*60*24*360);
			$this->Poll->save();
		}

		$this->valChoice->ValidationGroup		= "valChoice_" . $this->Parameters['id'];
		$this->btnAddChoice->ValidationGroup	= "valChoice_" . $this->Parameters['id'];
		$this->dataBind();
	}

	public function getPoll()
	{
		return $this->ConfigRecord;
	}

	public function setPoll($value)
	{
		$this->ConfigRecord = $value;
	}

	public function getPollChoices()
	{
		return $this->_pollchoices;
	}

	public function setPollChoices($value)
	{
		$this->_pollchoices	= TPropertyValue::ensureArray($value);
	}

	public function SavePollProperties($sender, $param)
	{
		if($this->Page->IsValid)
		{
			// Save the CSS information
			$barcolor	= $this->tbColor->Text;
			$expire		= strtotime($this->Expire->Date);
			$question	= $this->tbQuestion->Text;
			$active		= TPropertyValue::ensureInteger($this->Active->Checked);
			$this->DataAccess->createCommand("update tblpolls set question='$question',expire='$expire',barcolor='$barcolor',active='$active' where id=" . $this->Poll->id)->execute();
			$this->dataBind();
		}
	}

	public function AddChoice($sender, $param)
	{
		if($this->Page->IsValid)
		{
			$pollid			= $this->Poll->id;
			$answer			= $this->Choice->Text;
			$displayorder	= TPropertyValue::ensureInteger($this->DataAccess->createCommand("select max(displayorder) from tblpollchoices where pollid=$pollid")->queryScalar());
			$displayorder++;

			try
			{
				$this->DataAccess->createCommand("insert into tblpollchoices(pollid,answer,displayorder) values('$pollid','$answer','$displayorder')")->execute();
			}
			catch(Exception $e) {}
		}
		$this->Choice->Text	= '';
		$this->dataBind();
	}

	public function deletePollChoice($sender,$param)
	{
		$pollid	= $this->Poll->id;
		$id		= $this->dlPollChoices->DataKeys[$param->Item->ItemIndex];
		$this->DataAccess->createCommand("delete from tblpollchoices where id=$id")->execute();
		$this->dlPollChoices->SelectedItemIndex	= -1;
		$this->dlPollChoices->EditItemIndex		= -1;
		$this->dataBind();
	}

	public function itemCommand($s,$p)
	{
		switch($p->CommandName)
		{
			case "MoveDownPC":	$pollid = $this->MoveDownPC($p->CommandParameter); break;
			case "MoveUpPC":	$pollid = $this->MoveUpPC($p->CommandParameter); break;
		}

		$this->dataBind();
	}

	public function MoveUpPC($choiceid)
	{
		$sql	= "select * from tblpollchoices where id='$choiceid'";
		$row	= $this->DataAccess->createCommand($sql)->queryRow();

		if($row !== null)
		{
			$id	= $row['id'];
			$sql = "update tblpollchoices set displayorder=displayorder -1.001 where id=$id";
			$this->DataAccess->createCommand($sql)->execute();
			$this->RenumDisplayOrderPC($row['pollid']);
			$this->RenumDisplayOrderPC($row['pollid'],1);
		}
		return $row['pollid'];
	}

	public function MoveDownPC($choiceid)
	{
		$sql	= "select * from tblpollchoices where id='$choiceid'";
		$row	= $this->DataAccess->createCommand($sql)->queryRow();

		if($row !== null)
		{
			$id		= $row['id'];
			$sql = "update tblpollchoices set displayorder=displayorder +1.001 where id=$id";
			$this->DataAccess->createCommand($sql)->execute();
			$this->RenumDisplayOrderPC($row['pollid']);
			$this->RenumDisplayOrderPC($row['pollid'],1);
		}
		return $row['pollid'];
	}

	public function RenumDisplayOrderPC($pollid,$p_startat = 999.000)
	{
		$newdisplayorder	= $p_startat;
		$sql				= "select * from tblpollchoices where pollid='$pollid' order by displayorder";
		$rows				= $this->DataAccess->createCommand($sql)->query();
		if($rows)
		{
			foreach($rows as $row)
			{
				$sql = "update tblpollchoices set displayorder=$newdisplayorder where pollid='" . $row["id"] . "'";
				$this->DataAccess->createCommand($sql)->execute();
				$newdisplayorder++;
			}
		}
	}

	public function DeleteButtonClicked($sender,$param)
	{
		// Delete content from db
		$sql = "delete from tblpollchoices where pollid='" . $this->Poll->id . "'";
		$this->DataAccess->createCommand($sql)->execute();
		$sql = "delete from tblpolls where id='" . $this->Poll->id . "'";
		$this->DataAccess->createCommand($sql)->execute();
		parent::DeleteButtonClicked($sender,$param);
	}
}
?>